package m202405.d25;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/25 16:08
 */
public class Q17 {

    Map<Character, char[]> map = Map.of(
            '2', new char[]{'a', 'b', 'c'},
            '3', new char[]{'d', 'e', 'f'},
            '4', new char[]{'g', 'h', 'i'},
            '5', new char[]{'j', 'k', 'l'},
            '6', new char[]{'m', 'n', 'o'},
            '7', new char[]{'p', 'q', 'r', 's'},
            '8', new char[]{'t', 'u', 'v'},
            '9', new char[]{'w', 'x', 'y', 'z'}
    );

    public List<String> letterCombinations(String digits) {
        List<String> rs = new LinkedList<>();
        select(0, digits.toCharArray(), new StringBuilder(), rs);
        return rs;
    }

    private void select(int index, char[] chars, StringBuilder sb, List<String> rs) {
        if (index == chars.length) {
            if (!sb.isEmpty()) {
                rs.add(sb.toString());
            }
            return;
        }
        char[] value = map.get(chars[index]);
        for (char c : value) {
            sb.append(c);
            select(index + 1, chars, sb, rs);
            sb.deleteCharAt(index);
        }
    }
}
